package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.FanStatusItem;
import com.colorlight.cloud.complete.entity.po.PowerRelay;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设置im9风扇状态
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetIm9FanStatus implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 要设置的继电器状态，一次只设置一个多功能卡
         */
        private PowerRelay powerRelay;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 发送卡序号
         */
        private Integer prcIndex;

        /**
         * 网口号
         */
        private Integer portIndex;

        /**
         * 多功能卡序号
         */
        private Integer functionIndex;

        /**
         * 风扇状态
         */
        private List<FanStatusItem> fanStatusItem;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetIm9FanStatus.InputMsg.Builder inputMsg =
                CompleteInstruction.SetIm9FanStatus.InputMsg.newBuilder();
        SetIm9FanStatus.InputMsg thisInput = this.getInput();
        inputMsg.setPowerRelay(thisInput.getPowerRelay().toProto());
        final CompleteInstruction.SetIm9FanStatus.Builder builder =
                CompleteInstruction.SetIm9FanStatus.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetIm9FanStatus byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetIm9FanStatus setIm9FanStatus = null;
        try {
            setIm9FanStatus = CompleteInstruction.SetIm9FanStatus.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setIm9FanStatus)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetIm9FanStatus.OutputMsg outputMsg = new SetIm9FanStatus.OutputMsg();
        CompleteInstruction.SetIm9FanStatus.OutputMsg output = setIm9FanStatus.getOutput();
        outputMsg.setPrcIndex(output.getPrcIndex());
        outputMsg.setPortIndex(output.getPortIndex());
        outputMsg.setFunctionIndex(output.getFunctionIndex());
        outputMsg.setFanStatusItem(output.getFanStatusItemList().stream().map(p -> new FanStatusItem().formProto(p)).collect(Collectors.toList()));
        this.setOutput(outputMsg);
        return this;
    }
}

